@extends('layouts.default')
@section('content')
<?php
$stt = 0;
$txtLoaiBai = 'all';
$txtNoiDungTK = '';
$isFilterLoaiBai = false;
$isTK = false;

if (Input::has('page')) {
    $page = Input::get('page');
    $stt = ($page - 1) * QUAN_LY_SO_BAI_VIET_TREN_TRANG;
}
if (Input::has('txtLoaiBai')) {
    $txtLoaiBai = Input::get('txtLoaiBai');
    $isFilterLoaiBai = true;
}
if (Input::has('txtTimKiem')) {
    $txtNoiDungTK = Input::get('txtTimKiem');
    $isTK = true;
}
if(Session::has('kq'))
{
    if(Session::get('kq') == 'fail')
    {
    echo "<script>alert('Thay đổi trạng thái hiển thị không thành công!!!');</script>";
    }
}
?>
<!-- Content Table -->
<div class="row-fluid">
    <div class="box span12">
            <div class="box">
                <div class="box-header">
                    <h2>
                        <i class="icon-list"></i>
                        <span class="break"></span>Danh Sách Các Bài Viết
                    </h2>
                    <div class="box-icon">
                        <a href="#" class="btn-minimize"><i class="icon-chevron-up"></i></a>
                    </div>
                </div>
                <div class="box-content">
                    <div class="input-group">
                    <?php
                    if ($isTK || $isFilterLoaiBai) {
                        ?>
                    <input type="button" name="btnVeTrangChinh" id="btnVeTrangChinhId" class="btn btn-prev" onclick="location.href='<?= URL; ?>mthreads/listthreads'" value="Trang chủ QL bài viết"/>&nbsp;&nbsp;
                        <?php
                    }
                    ?>
                    <input type="text" name='txtNoiDungTK' id='txtNoiDungTKId' class="form-control" placeholder="Nhập nội dung bài viết muốn tìm" value="<?php if ($isTK) echo $txtNoiDungTK; ?>" style='width: 400px'/>&nbsp;&nbsp;
                    <input type="button" name="btnTK" id="btnTKId" value="Tìm kiếm" alt="Tìm kiếm" class='btn btn-primary' onclick="return thucHienTK((document.getElementById('txtNoiDungTKId').value))">&nbsp;&nbsp;
                    Loại bài viết
                    <select class="form-control" style="width:220px!important;" name="txtLoaiBaiViet" id="txtLoaiBaiVietId" onchange="thucHienLoc(document.getElementById('txtLoaiBaiVietId').value)">
                        <option value="all" id="allId" <?php if ($txtLoaiBai == 'all') echo "selected='selected'"; ?>>--- Tất cả ---</option>
                        <?php
                        foreach ($loaiBai as $lb) {
                            ?>
                            <option value="<?= $lb->id; ?>" id="loai_<?= $lb->id; ?>" <?php if ($txtLoaiBai == $lb->id) echo "selected='selected'"; ?>><?= $lb->mo_ta_loai_bai_viet; ?></option>
                            <?php
                        }
                        ?>
                    </select>
                    <input type="button" name="btnAddThread" id="btnAddThreadId" value="Thêm bài viết" class='btn btn-primary' onclick="location.href = '<?= URL ?>mthreads/addthreads'"/>
                    </div>
                    <table class="table table-striped table-bordered bootstrap-datatable datatable dataTable" aria-describedby="DataTables_Table_0_info" style="text-align: center;">
                        <tr>
                            <td width="5%">
                                STT
                            </td>
                            <td width="20%">
                                Tiêu đề bài viết
                            </td>
                            <td width="30%">
                                Tóm lược bài viết
                            </td>
                            <td width="13%">
                                Loại bài viết
                            </td>
                            <td width="7%">
                                Hiển thị
                            </td>
                            <?php
                             if (!$isTK) {
                                 ?>
                            <td width="10%">
                                Thứ tự
                            </td>
                                <?php
                              }
                              ?>
                            <td width="8%">
                                Sửa
                            </td>
                            <td width="7%">
                                Xóa
                            </td>
                        </tr>
                        <?php
                        if(count($dsBaiViet) !=0){
                        $i = 1;
                        foreach ($dsBaiViet as $baiViet) {
                            ?>
                            <tr>
                                <td>
                                    <?= ++$stt; ?>
                                </td>
                                <td>
                                    <?= html_entity_decode($baiViet->tieu_de_bai_viet); ?>
                                </td>
                                <td>
                                    <?= html_entity_decode($baiViet->tom_tat_bai_viet); ?>
                                </td>
                                <td>
                                    <?= $baiViet->mo_ta_loai_bai_viet; ?>
                                </td>
                                <td>
                                    <input type="checkbox" value="" name="checkHienThi" id="checkHienThiId_<?= $baiViet->id; ?>" <?php if ($baiViet->trang_thai_bai_viet) echo "checked='checked'"; ?> onclick="if(confirm('Xác nhận thay đổi trạng thái hiển thị bài viết này?')){hienThi(<?= $baiViet->id . "," . $baiViet->trang_thai_bai_viet; ?>);}"/>
                                </td>
                                 <?php
                             if (!$isTK) {
                                 ?>
                                <td><?php
                                    if ($stt != 1) {
                                        ?>
                                        <img src="{{URL}}public/images/icon/Up.png" border="0" style="cursor:hand" onclick="thuTu(<?= $baiViet->id . "," . $baiViet->thu_tu_bai_viet . "," . $dsThuTu[$stt - 2]->id . "," . $dsThuTu[$stt - 2]->thu_tu_bai_viet; ?>)" alt="Lên"/>
                                        <?php
                                    } else {
                                        echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                                    };
                                    if ($stt < $count) {
                                        ?>
                                        &nbsp;<img src="{{URL}}public/images/icon/Down.png" border="0" onclick="thuTu(<?= $baiViet->id . "," . $baiViet->thu_tu_bai_viet . "," . $dsThuTu[$stt]->id . "," . $dsThuTu[$stt]->thu_tu_bai_viet; ?>)" style="cursor:hand" alt="Xuống"/>
                                        <?php
                                    } else {
                                        echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                                    };
                                    ?>
                                </td>
                                <?php
                             }
                                ?>
                                <td>
                                    <a onclick="thucHienSua(<?= $baiViet->id; ?>)"><img src="{{URL}}public/images/icon/edit.gif" alt="Sửa"/></a>
                                </td>
                                <td>
                                    <img src="{{URL}}public/images/icon/delete.gif" alt="Xóa" onclick="if (confirm('Xác nhận xóa bài viết mã số <?= $baiViet->id; ?>?')) {
                                                thucHienXoa(<?= $baiViet->id; ?>)
                                            }"/>
                                </td>
                            </tr>
                            <?php
                            $i = $stt - 1;
                        }
                        ?>
                    </table>
                    <div class="row">
                        <div class="col-lg-12 center">
                            <div class="dataTables_paginate paging_bootstrap">
                                <?php
                                if (!$isTK && $isFilterLoaiBai) {
                                    echo $dsBaiViet->appends(array('txtLoaiBai' => $txtLoaiBai))->links();
                                } else if ($isTK && !$isFilterLoaiBai) {
                                    echo $dsBaiViet->appends(array('txtTimKiem' => $txtNoiDungTK))->links();
                                } else if ($isTK && $isFilterLoaiBai) {
                                    echo $dsBaiViet->appends(array('txtTimKiem' => $txtNoiDungTK, 'txtLoaiBai' => $txtLoaiBai))->links();
                                } else {
                                    echo $dsBaiViet->links();
                                }
                                ?>
                            </div>
                        </div>
                    </div>
                    <?php
                        }else{
                            ?>
                </table>
                  <div class="row">
                      <div class="col-lg-12">
                          <div class="center">
                              <i>Không có bài viết nào.</i>
                          </div>
                      </div>
                  </div>
                            <?php
                        }
                    ?>
                </div>
            </div>
    </div>
</div>
<!-- End Content Table -->
<!-- Form -->
<form name="formXoa" id="frmXoaId" method="post" action="<?= URL; ?>deletethreads">
    <?= Form::token(); ?>
    <input type="hidden" name="txtIdThread" id="txtIdThreadsId" value="" />
</form>
<form name="formEdit" id="formEditId" method="get" action="<?= URL; ?>mthreads/editthread">
    <?= Form::token(); ?>
    <input type="hidden" name="txtIdBaiViet" id="txtIdBaiVietId" value="" />
</form>
<form name="frmTK" id="frmTKId" method="get" action="<?= URL; ?>mthreads/listthreads">
    <input type="hidden" name="txtTimKiem" id="txtTimKiemId" value=""/>
    <?php
    echo Form::token();
    if ($isFilterLoaiBai) {
        ?>
        <input type="hidden" name="txtLoaiBai" id="txtLoaiBaiId" value="<?= $txtLoaiBai; ?>" />
        <?php
    }
    ?>
</form>
<form name="frmLoc" id="frmLocId" method="get" action="<?= URL; ?>mthreads/listthreads">
    <?php
    echo Form::token();
    if ($isTK) {
        ?>
        <input type="hidden" name="txtTimKiem" id="txtTimKiemId" value="<?= $txtNoiDungTK; ?>"/>
        <?php
    }
    ?>
    <input type="hidden" name="txtLoaiBai" id="txtLoaiBaiId" value="" />       
</form>
<form name="frmThuTu" id="frmThuTuId" method="post" action="<?= URL; ?>mthreads/thuTuThreads">
    <?= Form::token(); ?>
    <input type="hidden" name="txtIdThread" id="txtIdThreadId" value="" />
    <input type="hidden" name="txtIdThreadKe" id="txtIdThreadKeId" value="" />
    <input type="hidden" name="txtThuTuThread" id="txtThuTuThreadId" value="" />
    <input type="hidden" name="txtThuTuThreadKe" id="txtThuTuThreadKeId" value="" />
</form>
<form name="frmHienThi" id="frmHienThiId" method="post" action="<?= URL; ?>mthreads/hienThiThreads">
    <?= Form::token(); ?>
    <input type="hidden" name="txtMaSoBaiViet" id="txtMaSoBaiVietId" value="" />
    <input type="hidden" name="txtTrangThaiHienThi" id="txtTrangThaiHienThiId" value="" />
</form>
<!-- End Form -->
<script type='text/javascript'>
    function thucHienXoa(id) {
        document.formXoa.txtIdThread.value = id;
        document.formXoa.submit();
    }
    function thucHienSua(id) {
        document.formEdit.txtIdBaiViet.value = id;
        document.formEdit.submit();
    }
    function thucHienLoc(idLoai) {
        document.frmLoc.txtLoaiBaiId.value = idLoai;
        document.frmLoc.submit();
    }
    function thucHienTK(TK) {
        var txtTK = trim(TK);
        if ((txtTK == "" || txtTK == null))
        {
            alert("Xin nhập nội dung cần tìm kiếm!");
            document.getElementById('txtNoiDungTKId').focus();
            return false;
        }
        else {
            document.frmTK.txtTimKiem.value = txtTK;
            document.frmTK.submit();
            return true;
        }
        return false;
    }
    function thuTu(id, thutu, idKe, thutuKe) {
        document.frmThuTu.txtIdThread.value = id;
        document.frmThuTu.txtIdThreadKe.value = idKe;
        document.frmThuTu.txtThuTuThread.value = thutu;
        document.frmThuTu.txtThuTuThreadKe.value = thutuKe;
        document.frmThuTu.submit();
    }
    function hienThi(idbv,ttbv){
        document.frmHienThi.txtMaSoBaiViet.value = idbv;
        document.frmHienThi.txtTrangThaiHienThi.value = ttbv;
        document.frmHienThi.submit();
    }
</script>
@stop